package com.ideal.frame.node.controller;

import com.ideal.frame.common.dto.ResponseModel;
import com.ideal.frame.node.dto.NodeSearchForm;
import com.ideal.frame.node.entity.NodeTb;
import com.ideal.frame.node.form.search.NodeSyncParams;
import com.ideal.frame.node.service.NodeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Auther: liudingsheng
 * @Date: 2021/8/27
 * @Description:
 */
@RestController
@RequestMapping("/node")
public class NodeController {

    @Autowired
    private NodeService nodeService;

    @PostMapping("/page")
    public ResponseModel page(@RequestBody NodeSearchForm form) {
        return ResponseModel.success(nodeService.getPage(form));
    }

    @GetMapping("/getById/{nodeId}")
    public ResponseModel getById(@PathVariable Long nodeId) {
        return ResponseModel.success(nodeService.getById(nodeId));
    }

    @PostMapping("/saveNode")
    public ResponseModel saveNode(@RequestBody NodeTb nodeTb) {
        nodeService.saveNode(nodeTb);
        return ResponseModel.success();
    }

    @GetMapping("/checkNodeHasPrefix")
    public ResponseModel checkNodeHasPrefix(@RequestParam(required = false) Long nodeId, @RequestParam Long prefixId) {
        return ResponseModel.success(nodeService.checkNodeHasPrefix(prefixId, nodeId));
    }

    @DeleteMapping("/delete/{id}")
    public ResponseModel delete(@PathVariable Long id) {
        nodeService.deleteNode(id);
        return ResponseModel.success();
    }

    @PostMapping("/clientNotify/{nodeId}")
    public ResponseModel clientNotify(@PathVariable Long nodeId) {
        nodeService.clientNotify(nodeId);
        return ResponseModel.success();
    }

    @GetMapping("/getAllNotHasCerNodes")
    public ResponseModel getAllNotHasCerNodes() {
        return ResponseModel.success(nodeService.getAllNotHasCerNodes());
    }

    /**
     * 查询当前节点信息
     *
     * @return
     */
    @GetMapping("/getCurrentNode")
    public ResponseModel<NodeTb> getCurrentNode() {
        return ResponseModel.success(nodeService.getByLevel());
    }

    /**
     * 下级节点数
     *
     * @return
     */
    @GetMapping("/getNextNodeCount")
    public ResponseModel getNextNodeCount() {
        return ResponseModel.success(nodeService.getNextNodeCount());
    }


    @PostMapping("/joinInMonitor/{nodeId}")
    public ResponseModel joinInMonitor(@PathVariable Long nodeId) {
        nodeService.joinInMonitor(nodeId);
        return ResponseModel.success();
    }

    @PostMapping("/excludeMonitor/{nodeId}")
    public ResponseModel excludeMonitor(@PathVariable Long nodeId) {
        nodeService.excludeMonitor(nodeId);
        return ResponseModel.success();
    }

    /**
     * 选择节点
     *
     * @return
     */
    @GetMapping("/chooseVirtualNode")
    public ResponseModel chooseVirtualNode(@RequestParam(value = "nodeId") Long nodeId) {
        nodeService.chooseVirtualNode(nodeId);
        return ResponseModel.success();
    }

    /**
     * 查询当前选择的虚节点id
     *
     * @return 虚节点id
     */
    @GetMapping("/getSelectedVirtualNodeId")
    public ResponseModel getSelectedVirtualNodeId() {
        return ResponseModel.success(nodeService.getSelectedVirtualNodeId());
    }

    /**
     * 获取最新的同步数据
     *
     * @param params
     * @return
     */
    @ApiOperation(value = "获取需要同步的数据")
    @PostMapping("/sync/findSyncNodeData")
    public ResponseModel findSyncNodeData(@RequestBody NodeSyncParams params) {
        return ResponseModel.success(nodeService.findOtherSyncNodeData(params));
    }

    @ApiOperation(value = "同步节点数据")
    @PostMapping("/sync/syncNodeData")
    public ResponseModel syncNodeData(@RequestBody List<NodeTb> list) {
        nodeService.syncNodeData(list);
        return ResponseModel.success();
    }
}
